package link;

/**
 * 链表的实现
 * Created by zhaoxin on 2018/7/16.
 */
public class LinkedList<E> {

    private class Node{
        public E e;
        public Node next;

        public Node(E e, Node next){
            this.e = e;
            this.next = next;
        }

        public Node(E e){
            this(e, null);
        }

        public Node(){
            this(null, null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "e=" + e +
                    ", next=" + next +
                    '}';
        }
    }

    private Node dummyHead;  // 链表头节点
    private int size;   // 链表的长度

    public LinkedList(){
        dummyHead = new Node(null, null);
        size = 0;
    }

    /**
     * 判断链表的长度
     * @return
     */
    public int getSize(){
        return size;
    }

    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty(){
        return size == 0;
    }



    /**
     * 在index位置添加元素
     * @param index
     * @param e
     */
    public void add(int index, E e){
        if (index < 0 && index > size){
            throw new IllegalArgumentException("元素插入位置有误！");
        }
            // 因为始终prev指向需要添加节点的前一个节点。
            Node prev = dummyHead;
            for (int i = 0; i < index; i++) {
                // 循环遍历将prev指向链表前一个节点
                prev = prev.next;
            }
            Node newNode = new Node(e);
            newNode.next = prev.next;
            prev.next = newNode;
            size ++;
    }

    /**
     * 返回指定位置链表节点数据
     * @param index
     * @return
     */
    public E get(int index){
        if (index < 0 && index > size){
            throw new IllegalArgumentException("参数索引不合法！");
        }
        Node prev = dummyHead.next;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        return prev.e;
    }

    /**
     * 返回首节点数据
     * @return
     */
    public E getFirst(){
        return dummyHead.next.e ;
    }

    /**
     * 返回尾节点数据
     * @return
     */
    public E getLast(){
        return get(size - 1);
    }

    /**
     * 修改指定位置的元素
     * @param index
     * @param e
     */
    public void set(int index, E e){
        if (index < 0 && index >= size){
            throw new IllegalArgumentException("参数错误！");
        }
        Node prev = dummyHead.next;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.e = e;
    }

    /**
     * 链表中是否包含指定元素
     * @param e
     * @return
     */
    public boolean contain(E e){
        Node cur = dummyHead.next;
        for (int i = 1; i < size; i++) {
            if (e.equals(cur.e)){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    /**
     * 向链表头部添加元素
     */
    public void addFirst(E e){
        add(0, e);
    }

    /**
     * 添加元素到链表末尾
     * @param e
     */
    public void addLast(E e){
        add(size, e);
    }

    /**
     * 删除指定位置索引
     * @param index
     * @return
     */
    public E remove(int index){
        if (index < 0 && index > size)
            throw new IllegalArgumentException("参数不正确！");

        Node cur = dummyHead;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        Node deletedNode = cur.next;
        cur.next = deletedNode.next;
        deletedNode.next = null;
        size--; // 切记维护size
        return deletedNode.e;
    }

    /**
     * 移除链表首节点
     * @return
     */
    public E removeFirst(){
        return remove(0);
    }

    /**
     * 移除链表末尾元素
     * @return
     */
    public E removeLast(){
        return remove(size - 1);
    }
    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null){
            stringBuilder.append(cur.e);
            stringBuilder.append("->");
            cur = cur.next;
        }
        stringBuilder.append("NULL");
        return stringBuilder.toString();
    }
}
